/**
 * Created by Tim on 2017/5/23.
 */
$(function () {
    function PlatformMgr() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');
        // 初始化
        this.init();
    }

    PlatformMgr.prototype.Constructor = PlatformMgr;

    PlatformMgr.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    PlatformMgr.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource":"/admin/platform/page",
            "aoColumns": [
                {"sWidth": "8%", "sTitle": "名称", "sClass": "center", "mData": "name", "bSortable": false},
                {"sWidth": "18%", "sTitle": "地址前缀", "sClass": "center", "mData": "urlPrefix", "bSortable": false},
                {"sWidth": "8%", "sTitle": "描述", "sClass": "center", "mData": "description", "bSortable": false},
                {"sWidth": "8%", "sTitle": "创建时间", "sClass": "center", "mData": "created", "bSortable": false},
                {"sWidth": "16%", "sTitle": "操作", "sClass": "center", "mData": "id", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];

                if (aData['created']) {
                    $('td:eq(3)', nRow).html(SysTool.Date.formatDate(aData['created'], 'yyyy-MM-dd hh:mm:ss'));
                }

                //操作
                var option = '<a title="查询订单详细信息" href="' + "" + '/resource/list" class="btn btn-xs btn-primary">相关资源</a>';
                option += '&nbsp;<a title="查询下单用户档案信息" href="' + "" + '/archive/home/' + id + '" class="btn btn-xs btn-success">档案查询</a>';


                $('td:eq(4)', nRow).html(option);
            },
            "fnServerParams": function (aoData) {
                queryCondition("#search-form", aoData);
            },
            "fnStateLoaded": function (oSettings, oData) {
//                alert(JSON.stringify(oData));
            }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
        }
    };

    PlatformMgr.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    PlatformMgr.prototype.registEvent = function () {
        var that = this;
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.$btn_search_clear.click(function () {

            that.$seachr_btn.click();
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.DistributorMgr = new PlatformMgr();
});